package com.foreveross.bsl.mdm.ios;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;

import com.foreveross.bsl.mdm.Command;
import com.foreveross.bsl.mdm.ios.cmds.ClearPasscodeCommand;
import com.foreveross.bsl.mdm.ios.cmds.DeviceInformationCommand;
import com.foreveross.bsl.mdm.ios.cmds.DeviceLockCommand;
import com.foreveross.bsl.mdm.ios.cmds.EraseDeviceCommand;
import com.foreveross.bsl.mdm.ios.cmds.InstallApplicationCommand;
import com.foreveross.bsl.mdm.ios.cmds.InstalledApplicationListCommand;

/**
 * @description ios MDM指令工程
 * 
 * @author heyaowen
 * @version v1.0
 * 
 * @2014-7-21
 * 
 */
public class CommandFactory {
	
	/**
	 *  获取设备信息
	 * @return
	 */
	public static Command createDeviceInformationCommand() {
		DeviceInformationCommand deviceInformationCommand = new DeviceInformationCommand();
		return deviceInformationCommand;
	}
	
	
	/** 锁定设备
	 * @param pin
	 * @param message
	 * @param phoneNumber
	 * @return
	 */
	public static Command createDeviceLockCommand(String pin, String message, String phoneNumber) {
		DeviceLockCommand deviceLockCommand = new DeviceLockCommand();
		if (pin != null && !pin.isEmpty())
			deviceLockCommand.setPin(pin); // 假如是OS X则需要pin，才能锁定
		if(message != null && !message.isEmpty())
			deviceLockCommand.setMessage(message);
		if(phoneNumber != null && !phoneNumber.isEmpty())
			deviceLockCommand.setPhoneNumber(phoneNumber);
		return deviceLockCommand;
	}
	
	/** 清除密码
	 * @param data
	 * @return
	 */
	public static Command createClearPasscodeCommand(byte[] unlockToken) {
		if (unlockToken == null)
			return null;
		ClearPasscodeCommand clearPasscodeCommand = new ClearPasscodeCommand();
		clearPasscodeCommand.setUnlockToken(unlockToken);
		return clearPasscodeCommand;
	}
	/** 清除密码
	 * @param data
	 * @return
	 */
	public static Command createClearPasscodeCommand(String hexData) {
		if (hexData == null || hexData.isEmpty())
			return null;

		ClearPasscodeCommand clearPasscodeCommand = new ClearPasscodeCommand();
		try {
			clearPasscodeCommand.setUnlockToken(Hex.decodeHex(hexData.toCharArray()));
		} catch (DecoderException e) {
			e.printStackTrace();
		}
		return clearPasscodeCommand;
	}
	
	/** 擦除设备数据
	 *  
	 * @return
	 */
	public static Command createEraseDeviceCommand(String pin) {
		EraseDeviceCommand eraseDeviceCommand = new EraseDeviceCommand();
		if(pin != null && !pin.isEmpty())
			eraseDeviceCommand.setPin(pin);
		return eraseDeviceCommand;
	}
	
	/**  获取安装列表
	 * @param identifies
	 * @return
	 */
	public static Command createInstalledApplicationListCommand(String identifies, Boolean managedAppsOnly) {
		InstalledApplicationListCommand command = new InstalledApplicationListCommand();
		if (identifies != null && !identifies.isEmpty()) {
			for (String iden : identifies.replaceAll("^@|@$", "").split("@")) {
				command.addIdentifier(iden);
			}
		}
		if(managedAppsOnly != null)
			command.setManagedAppsOnly(managedAppsOnly);
		return command;
	}
	
	/** 安装第三方程序
	 * @param iTunesStoreID
	 * @return
	 */
	public static Command createInstallApplicationByiTunesStoreID(String iTunesStoreID) {
		InstallApplicationCommand installApplicationCommand = new InstallApplicationCommand();
		installApplicationCommand.setiTunesStoreID(iTunesStoreID);
		installApplicationCommand.setManagementFlags(4);
		return installApplicationCommand;
	}

	/*public static Command createRemoveApplicationCommandByIdentifier(String identifier) {
		RemoveApplicationCommand removeApplicationCommand = new RemoveApplicationCommand();
		removeApplicationCommand.setIdentifier(identifier);
		return removeApplicationCommand;
	}

	public static Command createProfileListCommand() {
		ProfileListCommand profileListCommand = new ProfileListCommand();

		return profileListCommand;
	}

	public static Command createInstallProfileCommand(byte[] bytes) {
		InstallProfileCommand installProfileCommand = new InstallProfileCommand();
		installProfileCommand.setPayload(bytes);
		return installProfileCommand;
	}

	public static Command createInstallProfileCommand(File file) {
		InstallProfileCommand installProfileCommand = new InstallProfileCommand();
		try {
			installProfileCommand.setPayload(file);
		} catch (IOException e) {
			e.printStackTrace();
		}

		return installProfileCommand;
	}

	public static Command createRemoveProfileCommand(String identifier) {
		RemoveProfileCommand removeProfileCommand = new RemoveProfileCommand();
		removeProfileCommand.setIdentifier(identifier);

		return removeProfileCommand;
	}

	public static Command createRestrictionsCommand() {

		RestrictionsCommand restrictionsCommand = new RestrictionsCommand();
		return restrictionsCommand;
	}

	public static Command createManagedApplicationListCommand(Collection<String> identifies) {
		ManagedApplicationListCommand command = new ManagedApplicationListCommand();
		if (identifies != null && !identifies.isEmpty()) {
			for (String iden : identifies) {
				command.addIdentifier(iden);
			}
		}
		return command;
	}*/
}
